package com.coder4j.biz.controller;

import com.coder4j.biz.entity.AiWanxiangEntity;
import com.coder4j.biz.service.AiWanxiangService;
import com.coder4j.biz.vo.AiWanxiangQuery;
import com.coder4j.biz.vo.AiWanxiangVO;
import com.coder4j.biz.vo.AiWanxiangEO;
import com.coder4j.log.OperationLog;
import com.coder4j.starter.tools.enums.StatusEnums;
import com.coder4j.starter.tools.util.DateUtils;
import com.coder4j.starter.tools.util.FileUtils;
import com.coder4j.starter.web.api.ApiNote;
import com.coder4j.starter.mybatis.BasePage;
import com.coder4j.starter.tools.vo.R;
import com.coder4j.starter.web.util.EasyExcelUtils;
import com.coder4j.starter.tools.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * 通义万相
 * @author keller
 * @date 2024-12-09 15:39:06
 */
@Slf4j
@RestController
@RequestMapping("/aiWanxiang")
public class AiWanxiangController {
    @Resource
    private AiWanxiangService service;

    @Resource
    private HttpServletResponse response;



    /**
     * 按ID查询通义万相
     * @param id AiWanxiang.id
     * @return 通义万相对象
     */
    @GetMapping("/getById")
    @ApiNote(name="按ID查询通义万相",createName = "keller",createTime = "2024-12-09 15:39:06")
    public R<AiWanxiangVO> getById( Long id){

        return R.success(service.baseGetVOById(id));
    }



    /**
     * 保存通义万相
     * 有 id 字段，更新；否则新增
     * @param vo AiWanxiang对象
     * @return AiWanxiang对象
     */
    @PostMapping("/save")
    @ApiNote(name="保存通义万相",createName = "keller",createTime = "2024-12-09 15:39:06")
    public R save(@RequestBody AiWanxiangVO vo){

        if(ObjectUtils.hasEmpty(vo.getPrompt())){
            return R.paramError();
        }
        if(vo.getId() == null){
            vo.setState(StatusEnums.Waiting.name());
        }
        service.baseSaveVO(vo);
        return R.success();
    }

    /**
     * 查询列表
     * @param query 查询条件
     * @return 数据列表
     */
    @PostMapping("/page")
    @OperationLog("查看列表")
    @ApiNote(name="查询通义万相列表",createName = "keller",createTime = "2024-12-09 15:39:06")
    public R<BasePage<AiWanxiangVO>> getList(@RequestBody AiWanxiangQuery query){
        if(query == null){
            query = new AiWanxiangQuery();
        }
        return R.success(service.baseQueryVOPage(query));
    }

    /**
     * 导出列表
     * @param query 查询条件
     */
    @PostMapping("/export")
    @ApiNote(name="导出通义万相列表",createName = "keller",createTime = "2024-12-09 15:39:06")
    public void export(@RequestBody AiWanxiangQuery query){
        if(query == null){
            query = new AiWanxiangQuery();
        }
        List<AiWanxiangEntity> list = service.baseQueryList(query);
                List<AiWanxiangEO> eoList = new ArrayList<>();
                for (AiWanxiangEntity entity : list) {
                    AiWanxiangEO eo = new AiWanxiangEO();
                    BeanUtils.copyProperties(entity,eo);
                    eoList.add(eo);
                }
                EasyExcelUtils.exportClass(response,"AiWanxiangList",eoList);
    }


    /**
     * 删除通义万相
     * 如果Entity继承了 BaseSoftDeleteEntity，使用逻辑删除；否则，使用物理删除
     * @param id AiWanxiang.id
     * @return 删除的数据
     */
    @PostMapping("/deleteById")
    @ApiNote(name="删除通义万相",createName = "keller",createTime = "2024-12-09 15:39:06")
    public R deleteById( Long id){
        service.baseDeleteById(id);
        return R.success();
    }


}
